package com.wyp168.eduOrder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyp168.commonutils.JwtUtils;
import com.wyp168.commonutils.R;
import com.wyp168.eduOrder.entity.Order;
import com.wyp168.eduOrder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author wyp168
 * @since 2022-06-03
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        System.out.println("&&&courseId:"  + courseId);
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //创建订单，返回订单号
        String orderId = orderService.createOrders(courseId,memberId);


        System.out.println("***orderId:" + orderId);
        return R.ok().data("orderId",orderId);
    }

    //根据课程id，查询订单信息
    @GetMapping("gerOrderInfo/{orderId}")
    public R gerOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order>  wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order orderInfo = orderService.getOne(wrapper);

        return R.ok().data("orderInfo",orderInfo);
    }

    //判断当前用户是否购买了该课程
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        return count>0;
    }

}

